<?php


namespace App\Http\Services\MeiTuan;


use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;

class SendRequest
{
    /**
     * @param $url
     * @param array $payload
     * @param string $appAuthToken
     * @return false|\Psr\Http\Message\ResponseInterface
     */
    public static function postRequest($url,array $payload,string $appAuthToken = ''){
        $payload = array_merge($payload, (new Config())->getSystemPayload());
        $payload = http_build_query(self::signPayload($payload));
        $client = new Client();
//        $response = $client->post($url, ['headers'=>['Content-Type' =>'application/x-www-form-urlencoded'],'body' => $payload]);
        try {
            $response = $client->post($url, ['headers'=>['Content-Type' =>'application/x-www-form-urlencoded'],'body' => $payload]);
        } catch (GuzzleException $e) {
            \Log::error("GuzzleExceptionERROR:postRequest in App\Http\Services\MeiTuan");
            \Log::error($e->getTraceAsString());
            return false;
        }
        return $response;
    }
    protected static function signPayload(array $payload): array
    {
        $sort   = ksort($payload);
        $str    = '';
        if ($sort){
            foreach ($payload as $key=>$value){
                if ( $key != '' && $key != 'sign' && $value != '')
                    $str = $str.$key.$value;
            }
            $config = new Config();
            $str=$config->getSignSecret().$str;
            $sign = [
                'sign'=> bin2hex(sha1($str,true))
            ];
            return array_merge($payload,$sign);
        }
        return [];
    }

    //服务商
    public static function spPostRequest($url,array $payload,string $appAuthToken = ''){
        $business_payload = [
            'biz'=>json_encode($payload,JSON_UNESCAPED_UNICODE)
        ];
        $data    = array_merge($business_payload, (new Config())->getServiceProviderPublicPayload());
        $payload = self::spSignPayload($data);
        \Log::notice('$payload With sign:=>'.$payload['sign']);

        $payload = http_build_query($payload);

        try {
            $client = new Client();
            $response = $client->post($url, ['headers'=>['Content-Type' =>'application/x-www-form-urlencoded'],'body' => $payload]);
        } catch (GuzzleException $e) {
            \Log::error("GuzzleExceptionERROR:spPostRequest in App\Http\Services\MeiTuan".$e->getMessage());
            \Log::error($e->getTraceAsString());
            return false;
        } catch (\Exception $exception){
            \Log::error("Exception:spPostRequest in App\Http\Services\MeiTuan--".$exception->getMessage());
            \Log::error($exception->getTraceAsString());
            return false;
        }
        return $response;
    }
    protected static function spSignPayload(array $payload): array
    {
        $sort   = ksort($payload);
        $str    = '';
        if ($sort){
            foreach ($payload as $key=>$value){
                if ( $key != '' && $key != 'sign' && $value != '')
                    $str = $str.$key.$value;
            }
            $config = new Config();
            $str=$config->getServiceProviderSignKey().$str;
            $sign = [
                'sign'=> bin2hex(sha1($str,true))
            ];
            return array_merge($payload,$sign);
        }
        return [];
    }

}
